const state = {
  videoNode: {
    dev: 0,
    devOffline: 0,
    company: 0,
    companyRate: 0,
    companyOffline: 0,
    devRate: 0,
    devStatusUp: false,
    devStatusDown: false,
    rateStatusUp: false,
    rateStatusDown: false,
    first: true
  }
}

const getters = {
  getVideoNode: state => {
    return state.videoNode
  }
}

const mutations = {
  resetVideoNode (state) {
    state.videoNode.dev = 0
    state.videoNode.devOffline = 0
    state.videoNode.company = 0
    state.videoNode.companyRate = 0
    state.videoNode.companyOffline = 0
    state.videoNode.devRate = 0
    state.videoNode.devStatusUp = false
    state.videoNode.devStatusDown = false
    state.videoNode.rateStatusUp = false
    state.videoNode.rateStatusDown = false
    state.videoNode.first = true
  },
  setVideoNode (state, payload) {
    let obj = {
      dev: 0,
      afterDev: 0,
      devOffline: 0,
      devRate: 0,
      rateStatus: 1,
      companyOffline: 0,
      company: 0,
      companyRate: 0
    }

    obj.companyOffline = payload.companytotal - payload.company
    if (payload.companytotal) {
      obj.companyRate = (payload.company / payload.companytotal).toFixed(2) * 100
    }

    obj.devOffline = payload.devtotal - payload.dev
    if (payload.devtotal) {
      obj.devRate = (payload.dev / payload.devtotal).toFixed(2) * 100
    }

    obj.afterDev = payload.dev - payload.beforeDev
    obj.company = payload.company
    if (obj.afterDev > 0) {
      obj.devStatusUp = true
      obj.devStatusDown = false
    } else if (obj.afterDev === 0) {
      obj.devStatusUp = false
      obj.devStatusDown = false
    } else {
      obj.devStatusUp = false
      obj.devStatusDown = true
    }

    let beforeRate = 0
    if (payload.beforeTotal) {
      beforeRate = (payload.beforeDev / payload.beforeTotal).toFixed(2) * 100
    }
    obj.afterRate = obj.devRate - beforeRate
    if (obj.afterRate > 0) {
      obj.rateStatusUp = true
      obj.rateStatusDown = false
    } else if (obj.afterRate === 0) {
      obj.rateStatusUp = false
      obj.rateStatusDown = false
    } else {
      obj.rateStatusUp = false
      obj.rateStatusDown = true
    }
    state.videoNode = {...obj, ...payload}
  },
  setVideoNodeCompany (state, payload) {
    state.videoNode = {...state.videoNode, ...payload}
  }
}

export default {
  state,
  getters,
  mutations
}
